const config = require('../config/config');
const pageConf = config.page_conf;

const ProductServer = require('../services/Product');
const { getProductData } = ProductServer;

class ShopController {
  
  /* 获得主页路由*/
  async getIndexPage(ctx, next) {
    //所有产品
    const prodsArr = await getProductData();
    //七牛图片前缀
    const qiniuImgUrl = config.qiniu.bucket.xingsheng.domain;
    // console.log(prodsArr);
    //渲染index主页ejs
    await ctx.render('shop/index', {
      pageConf: pageConf.index,
      prodsArr: prodsArr,
      qiniuImgUrl: qiniuImgUrl,
      path: '/index',
      swiperUrlData: [
        'https://aecpm.alicdn.com/simba/img/TB1CWf9KpXXXXbuXpXXSutbFXXX.jpg_q50.jpg',
        'https://aecpm.alicdn.com/simba/img/TB1CWf9KpXXXXbuXpXXSutbFXXX.jpg_q50.jpg',
        'https://aecpm.alicdn.com/simba/img/TB1CWf9KpXXXXbuXpXXSutbFXXX.jpg_q50.jpg'
      ]
    });
  }


   /* 获得主页数据*/
   async getIndexData(ctx, next) {
    //所有产品
    const prodsArr = await getProductData();  //这里要修改权限
    //七牛图片前缀
    const qiniuImgUrl = config.qiniu.bucket.xingsheng.domain;
    //组合首页需要的数据
    const resData = {
      indexData: pageConf.index,
      prodsArr: prodsArr,
      qiniuImgUrl
    }
    //渲染index主页数据
    ctx.body = JSON.stringify(resData);
  }

  /* 获取语音密钥  MW middleware*/
  async getUrtcKeyMW () {

    //渲染index主页数据
    ctx.body = { 
      status: 200,
      key: {
        
      }
    };
  }

   /* 获得详情页*/
   async getDetailPage(ctx, next) {
    const pid = ctx.params.pid;
    let prod = await ProductServer.getProductById(pid);

    //七牛图片前缀
    const qiniuImgUrl = config.qiniu.bucket.xingsheng.domain;

    // console.log(prod)
    //渲染detail页
    await ctx.render('shop/detail', {
      pageConf: pageConf.detail,
      prod: prod,
      qiniuImgUrl: qiniuImgUrl,
      path: 'shop/product-detail'
    });
    
  }



}

module.exports = new ShopController();